Get Email

This report returns the email addresses from customers who have contacted customer service ('custom_fields' value = 'customer_service') in a certain timeframe. Optional slicing by incoming channel.

Import Tickets


In [ ]:
### Read file where API token is stored
token = open('zd_token').read().strip('\n')
email = open('zd_email').read().strip('\n')
subdomain = open('zd_subdomain').read().strip('\n')

Import and save CX tickets in array.


In [ ]:
from zendesk import Zendesk
import datetime
import time
import json
import collections 
import csv

### Zendesk credentials
zd = Zendesk(subdomain,
             email+'/token',
             token) 

### Set the interval you're interested in
### today = datetime.datetime.today().date()
### weekStartDate = today
### weekEndDate = today

weekStartDate = (datetime.datetime.now() - datetime.timedelta(days = 34)).date()
weekEndDate = (datetime.datetime.now() - datetime.timedelta(days = 30)).date()

print '---------------------------------------------------------------------'
print 'START.'
print 'Week Start Date:', weekStartDate,'. Week End Date:', weekEndDate, '.'
print '---------------------------------------------------------------------'


### Build array containing tickets from given time intervall
tickets = []

try:
    pageNumber=1
    nextPage = True
    while nextPage:
        zdresult = zd.search(query='type:ticket created>='+ str(weekStartDate) +' created<=' + str(weekEndDate), page=pageNumber)
        print 'Zendesk Result Page Number: ', pageNumber
        print zdresult['next_page']
        for result in zdresult['results']:
            if result['custom_fields'][1]['value'] == 'customer_service': #adjust custom field according to your ticket structure
                tickets.append(result)
        if zdresult['next_page'] is None:
            nextPage=False
        else:
            nextPage=True
            pageNumber += 1
except ValueError:
    print(ValueError)

print '---------------------------------------------------------------------'    
print 'Tickets array length:', len(tickets) , '.'
print 'DONE.'
print '---------------------------------------------------------------------'

Use Print method below that suits best:

1) Print Emails (All Channels)


In [ ]:
requesterArr = []
noEmail = 0

print '---------------------------------------------------------------------'
print 'START.'
print '---------------------------------------------------------------------'

try:
    for ticket in tickets:
        ticketID = ticket['id']
        source = ticket['custom_fields'][9]['value'] #adjust custom field according to your ticket structure
        requesterID = ticket['requester_id'] 
        zdresult = zd.search(query='type:ticket user:'+ str(requesterID))
        email = zdresult['results'][0]['email']
        if (email is not None):
            requesterArr.append([ticketID,email,channel])
#             print 'ticket: ',  ticketID, ',',email , ',', channel            
            print ("'%s'," % email)
        elif:
            noEmail+=1;
                
except ValueError:
    print("Error!")

print '---------------------------------------------------------------------'    
print 'Length: ', len(requesterArr) , ', no email: ', noEmail , '.'
print 'DONE.'
print '---------------------------------------------------------------------'

2) Print Emails (Channel: Voice)


In [ ]:
requesterArr = []
callNoEmail = 0

print '---------------------------------------------------------------------'
print 'START.'
print '---------------------------------------------------------------------'

try:
    for ticket in tickets:
        channel = ticket['via']['channel']
        ticketID = ticket['id']
        source = ticket['custom_fields'][9]['value'] #adjust custom field according to your ticket structure
        requesterID = ticket['requester_id'] 
        zdresult = zd.search(query='type:ticket user:'+ str(requesterID))
        email = zdresult['results'][0]['email']
        if (email is not None) and (channel =='voice'):
            requesterArr.append([ticketID,email,channel])
#             print 'ticket: ',  ticketID, ',',email , ',', channel
            print ("'%s'," % email)
        elif (channel =='voice'):
            callNoEmail+=1;
                
except ValueError:
    print("Error!")

print '---------------------------------------------------------------------'
print 'Length: ', len(requesterArr) , ', call but no email: ', callNoEmail, '.'
print 'DONE.'
print '---------------------------------------------------------------------'

3) Print Emails (Channels: All minus Voice)


In [ ]:
requesterArr = []
noEmail = 0

print '---------------------------------------------------------------------'
print 'START.'
print '---------------------------------------------------------------------'

try:
    for ticket in tickets:
        channel = ticket['via']['channel']
        ticketID = ticket['id']
        source = ticket['custom_fields'][9]['value'] #adjust custom field according to your ticket structure
        requesterID = ticket['requester_id'] 
        zdresult = zd.search(query='type:ticket user:'+ str(requesterID))
        email = zdresult['results'][0]['email']
        if (email is not None) and (channel !='voice'):
            requesterArr.append([ticketID,email,channel])
#             print 'ticket: ',  ticketID, ',',email , ',', channel            
            print ("'%s'," % email)
        elif (channel !='voice'):
            noEmail+=1;
                
except ValueError:
    print("Error!")

print '---------------------------------------------------------------------'
print 'Length: ', len(requesterArr) , ', no email: ', noEmail, '.'
print 'DONE.'
print '---------------------------------------------------------------------'

In [ ]: